#!/usr/bin/env python3.10
# -*- encoding: utf-8 -*-
"""
@File    :   controllers.py
@Time    :   2024/02/06 11:50:30
@Author  :   windstarry 
@Version :   1.0
"""
# here put the import lib

from ninja_extra import api_controller, route, status
from ninja_extra.shortcuts import get_object_or_none,get_object_or_exception
from ninja_extra.permissions import IsAuthenticated
from ninja_jwt.authentication import JWTAuth

from .mixins import CategoryViewMixin
from .schema import CategorySchema, CreateCategorySchema
from core.schemas import ErrorSchema


@api_controller(
    "/category", tags=["分类管理"], permissions=[IsAuthenticated], auth=JWTAuth()
)
class CategoryController(CategoryViewMixin):
    @route.get(
        "/list",
        response=list[CategorySchema],
        summary="分类列表",
        url_name="category-list",
    )
    def category_list(self):
        """
        获取分类列表

        Returns:
            QuerySet: 分类查询集
        """
        return self.get_queryset()

    @route.get(
        "/info/{id}",
        response=CategorySchema,
        summary="分类详情",
        url_name="category-detail",
    )
    def get_category_detail(self, id: int):
        """
        根据分类id获取分类详情

        Args:
            id (int): 分类id

        Returns:
            obj: 分类对象
        """
        obj = self.get_object_or_exception(
            self.get_queryset(),
            id=id,
            error_message="分类ID不存在",
        )
        return obj

    @route.post(
        "/add",
        summary="新增分类",
        url_name="category-add",
        response=[(201, CategorySchema), (400, ErrorSchema)],
    )
    def create_category(self, category: CreateCategorySchema):
        """
        创建分类

        Args:
            category (CreateCategorySchema): 创建分类的请求对象

        Returns:
            tuple: 返回一个元组，包含状态码和响应内容

        Raises:
            Exception: 如果创建分类时出现异常
        """
        try:
            category = category.create_obj()
            return 201, category
        except Exception as ex:
            return 400, dict(details=str(ex))
        
    @route.delete(
        "/delete/{id}",
        summary="删除分类",
        url_name="category-delete",
        response={204: dict},
    )
    def  delete_category(self, id: int):
        obj = self.get_object_or_exception(
            self.get_queryset(),
            id=id,
            error_message="分类ID不存在",
        )
        obj.delete()
        return self.create_response("分类已删除", status_code=status.HTTP_204_NO_CONTENT)
    
    @route.put(
        "/update/{id}",
        summary="更新分类",
        url_name="category-update",
        response=CategorySchema,
    )
    def update_category(self, id: int, category: CreateCategorySchema):
        obj = self.get_object_or_exception(
            self.get_queryset(),
            id=id,
            error_message="分类ID不存在",
        )
        category.update(obj)
        return obj
